# -*- coding: utf-8 -*-
import json
import codecs
import sys
reload(sys)   
sys.setdefaultencoding('utf8') 
# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html


class FangPipeline(object):
	def __init__(self):
		self.file = codecs.open("E:\jiaocheng\F\Project\\fang\data\\fangzi_list.json",'wb',encoding = 'utf-8')


	def process_item(self, item, spider):
		print "*************格式化中*******"
		#for i in range(len(item['name'])):
		#名称
		name = item['name']
		#所在区域
		address = item['address']
		#楼栋总数
		sum_loudong = item['sum_loudong']

		#建筑年代
		build_year = item['build_year']

		#总户数
		sum_hushu = item['sum_hushu']

		#物业类型
		type_wuye = item['type_wuye']

		#容积率
		rongjilv = item['rongjilv']

		#绿化率
		green = item['green']

		#物业费
		wuye_price = item['wuye_price']

		#开发商
		build_name = item['build_name']

		#物业公司
		wuye_name = item['wuye_name']

		#小区地址
		place_xiaoqu = item['place_xiaoqu']

		#周边学校
		school = item['school']

		#小区均价
		price = item['price']

		#环比上月（上涨or下跌）
		com_last_month = item['com_last_month']

		#小区成交
		turnover = item['turnover']


		dic = {
				#名称
				"name" : name,
				#所在区域
				"address" : address,
				#楼栋总数
				"sum_loudong" : sum_loudong,
				#建筑年代
				"build_year" : build_year,
				#总户数
				"sum_hushu" : sum_hushu,
				#物业类型
				"type_wuye" : type_wuye,
				#容积率
				"rongjilv" : rongjilv,
				#绿化率
				"green" : green,
				#物业费
				"wuye_price" : wuye_price,
				#开发商
				"build_name" : build_name,
				#物业公司
				"wuye_name" : wuye_name,
				#小区地址
				"place_xiaoqu" : place_xiaoqu,
				#周边学校
				"school" : school,
				#小区均价
				"price"  : price,
				#环比上月（上涨or下跌）
				"com_last_month" : com_last_month,
				#小区成交
				"turnover" : turnover,
			}

		dicts = json.dumps(dict(dic),ensure_ascii=False)
		line = dicts + '\n'
		print "保存中。。。。。"
		self.file.write(line)

		return item

	def close_spider(self,spider):
		self.file.close()



"""
			name = item['name'][i]
			#所在区域
			address = item['address'][i]
			#楼栋总数
			sum_loudong = item['sum_loudong'][i]

			#建筑年代
			build_year = item['build_year'][i]

			#总户数
			sum_hushu = item['sum_hushu'][i]

			#物业类型
			type_wuye = item['type_wuye'][i]

			#容积率
			rongjilv = item['rongjilv'][i]

			#绿化率
			green = item['green'][i]

			#物业费
			wuye_price = item['wuye_price'][i]

			#开发商
			build_name = item['build_name'][i]

			#物业公司
			wuye_name = "NONE"#item['wuye_name'][i]

			#小区地址
			place_xiaoqu = item['place_xiaoqu'][i]

			#周边学校
			school = "NONE"#item['school'][i]

			#小区均价
			price = item['price'][i]

			#环比上月（上涨or下跌）
			com_last_month = item['com_last_month'][i]

			#小区成交
			turnover = item['turnover'][i]
"""

